<?php

namespace support\command;

use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class MakeCommandCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('make:command')
            ->addArgument('name', InputArgument::REQUIRED, 'command name')
            ->addArgument('path', InputArgument::OPTIONAL, 'file path under app');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $path = $input->getArgument('path');

        $suffix = 'Command';
        $defaultPathUnderApp = 'command';
        $className = ucfirst($name) . $suffix;
        $filename = $className . '.php';
        $relativePath = 'app/' . ($path ?: $defaultPathUnderApp);
        $relativeFilePath = $relativePath . DIRECTORY_SEPARATOR . $filename;
        $namespace = str_replace('/', '\\', $relativePath);
        $filePath = root_path() . $relativeFilePath;

        $output->writeln('make command' . $className);
        if (is_file($filePath)) {
            $helper = new QuestionHelper();
            $question = new ConfirmationQuestion("$relativeFilePath already exists. Do you want to override it? (yes/no)\n", false);
            if (!$helper->ask($input, $output, $question)) {
                $output->writeln('create ' . $filename . ' fail');
                return self::SUCCESS;
            }
        }
        $path = pathinfo($filePath, PATHINFO_DIRNAME);
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        $content = $this->getContent($className, $namespace);
        file_put_contents($filePath, $content);
        $output->writeln('create ' . $filename . ' success');
        return self::SUCCESS;
    }

    public function getContent($className, $namespace)
    {
        $stubPath = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR;
        $stubPath .= 'command.stub';
        $stub = file_get_contents($stubPath);
        return str_replace(
            ['{%className%}', '{%namespace%}'],
            [$className, $namespace],
            $stub
        );
        return <<<EOF
<?php

namespace $namespace;

use support\command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class $className extends Command
{
    public function configure()
    {
        \$this
            ->setName('')
            ->addArgument('', InputArgument::REQUIRED, '')
            ->addArgument('', InputArgument::OPTIONAL, '');
    }

    protected function execute(InputInterface \$input, OutputInterface \$output): int
    {
        \$error = false;
        if (!\$error) {
            \$helper = new QuestionHelper();
            \$question = new ConfirmationQuestion("error heppend. force execute? (yes/no)\\n", false);
            if (!\$helper->ask(\$input, \$output, \$question)) {
                \$output->writeln('error heppend. execute fail');
                return self::SUCCESS;
            }
        }
        return self::SUCCESS;
    }

}

EOF;
    }
}
